/**
 *  @file configManager.cpp
 *
 *  @date 24-dec-2009
 *  @author Ives van der Flaas
 */

#include <cassert>
#include <string>
#include "configManager.h"
#include "tinyxml.h"

namespace si
{

ConfigManager::ConfigManager():
	// These are the defaults
	fMuted(false)
{
	load();

}

ConfigManager::~ConfigManager()
{
	save();
}

void ConfigManager::save()
{
	TiXmlDocument doc;

	TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "","");
	doc.LinkEndChild(decl);

	TiXmlElement* root = new TiXmlElement("settings");
	doc.LinkEndChild(root);

	// write muted
	{
		TiXmlElement* mute = new TiXmlElement("muted");
		root->LinkEndChild(mute);

		TiXmlText* txt = new TiXmlText(fMuted ? "1": "0");
		mute->LinkEndChild(txt);
	}


	doc.SaveFile("config/settings.xml");
}

void ConfigManager::load()
{
	TiXmlDocument doc("config/settings.xml");

	// If we can't load the settingsfile, assume defaults...
	if(!doc.LoadFile())
		return;

	TiXmlDeclaration* decl = doc.FirstChild()->ToDeclaration();

	assert(std::string(decl->Version()) == std::string("1.0"));

	TiXmlElement* root = doc.FirstChildElement("settings");
	// Muted
	{
		std::string muted = root->FirstChildElement("muted")->GetText();
		fMuted = ((muted == "1" || muted == "true")? true : false);
	}


}

}
